<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragrma","no-cache");
response.setDateHeader("Expires",0);
%>
<title>_${customer.customerName}_预览任务</title>
<script type="text/javascript">

  //创建任务
  function createTask(i){
	 $("#btn_task_"+i).attr("disabled",true);
	 $("#btn_task_"+i).attr("class","btn");
    //验证是否库存满足
    var flag=true;
    var tempName="stockInfo_"+i;
    $("[name="+tempName+"]").each(function(){
        var orderno=$(this).attr("id").substring(10); 
        var htm=$(this).html(); 
        if(htm==''|| htm==undefined){
           $("#tr_"+orderno).css("background-color",'yellow');
           flag=false;
        }
    });
    
    if(flag==false){
		$("[name='taskPre']").css("display","none");
		$("#taskPre_"+i).css("display","table");
		$("[name='msg']").html("点击查看");
		$("#msg_"+i).html("点击隐藏");
       alert("黄色标识订单，库存不能满足，请核对...");
       return;
    }
       
    $("#btn_task_"+i).attr("disabled", true);
    var batchCode='${batch.batchTaskCode}';
    //封装拣货库区信息
    var jsonarray_workOrder=[];
    $('[name="task_'+i+'"]').each(function(){
        var ordeno=$(this).attr("id").substring(7);
        var stockInfo=$("#stockInfo_"+ordeno).html();
        var rankno=$("#rankno_"+ordeno).val();
        var obj=createItem(ordeno,stockInfo,rankno);
        jsonarray_workOrder.push(obj);
    });
    var jsonList_workOrder = eval(jsonarray_workOrder); 
	var jsonText_workOrder = JSON.stringify(jsonList_workOrder);
	
	//创建作业任务
	$.ajax({
			type:'post',
			url:'${ctx}/focusBackupTask/ajaxCreateBackupTask',
			data:{
				batchCode:batchCode,
				workOrderJsonText:jsonText_workOrder
			},
			dataType:'json',
			success:function(data) {
			    if(data.status==1){
			      $("#btn_task_"+i).attr("disabled", true);
				  $("#btn_task_"+i).attr("class", "btn");
				  $("#btn_task_"+i).html("已创建任务");
				  
				  //跳转
				  var flag=true;
				  $("[name='btn_createTask']").each(function(){
						 var varClass=$(this).attr("class");
						 if(varClass=='btn btn-primary'){
						     flag=false;
						 }
				  });
				  if(flag==true){
				      location.href="${ctx}/focusBatchTask/toBatchListPage";
				  }
				  
				  
			      
			    }else{
			      $("#btn_task_"+i).attr("disabled", false);
			      alert("网络繁忙，请稍后重试...");
			    }
			   
			},
			error:function() {
		    	$("#btn_task_"+i).attr("disabled",false);
				alert("网络繁忙，请稍后重试...");
			}
	});
  }
  
  //创建json对象
  function createItem(ordeno,stockInfo,rankno){
    return {
        ordeno:ordeno,
        stockInfo:stockInfo,
        rankno:rankno
    };
  }
  
  //显示or隐藏
  function showOrHide(i){
	  var temp=$("#taskPre_"+i);
	  var msg=$("#msg_"+i);
	  
	  if(temp.css("display")=="table"){
		  temp.css("display","none");
		  msg.html("点击查看");
	  }else{
		  $("[name='taskPre']").css("display","none");
		  temp.css("display","table");
		  
		  $("[name='msg']").html("点击查看");
		  msg.html("点击隐藏");
	  }
  }
  
  
</script>
</head>

<body>
	
	<table class="table  table-bordered table-condensed" style="margin-top: 0px;">
	<thead>
	   <th style="vertical-align: middle; color: white; height: 30px; " colspan="2" bgcolor="#8EB4CB">预览作业任务</th>
	</thead>
	<tbody>
	<c:if test="${empty taskList or taskList.size() eq 0}">
	       <tr><td colspan="2" height="50" style="vertical-align:middle; text-align: center;color: gray;font-size: 20px;">无数据</td></tr>
	</c:if>
	
	<c:forEach items="${taskList}" var="t" varStatus="i">
	<tr>
	  <td style='vertical-align: middle;'>
	            <font color="#145565"><b>作业任务【${i.count}】</b></font><a href='#' class="btn btn-small" onclick='showOrHide(${i.count});' id='msg_${i.count}' name='msg'>点击查看</a>
	  </td>
	  <td style='vertical-align: middle; color: white; width:100px;' >
	     <button  class='btn btn-primary'  onclick='createTask(${i.count})'  id='btn_task_${i.count}' name="btn_createTask" style="width: 100px;">确定创建</button>
	  </td>  
	</tr>
			
	<tr  >
	  <td colspan="2">
				<table  class='table table-bordered' style='display:none;'  id='taskPre_${i.count}' name='taskPre'>
				<thead><tr> <th>序号</th> <th>订单号</th> <th>SKU编码（商品数量）</th>  <th>库位_箱号_sku_数量</th> </tr></thead>
				<tbody>
				     <c:forEach items="${t.orderList}" var="o" varStatus="j">
				       <tr id="tr_${o.ordeno}"> 
					        <td>${j.count}</td> 
					        <td id='ordeno_${o.ordeno}' name='task_${i.count}' >${o.ordeno}</td> 
					        <td>${o.orderItems[0].sku}(1)</td> 
					        <td  id='stockInfo_${o.ordeno}' name="stockInfo_${i.count}">${o.stockInfo}</td> 
					        <input id="rankno_${o.ordeno}" type="hidden" value="${o.rankno}">
				       </tr>
				     </c:forEach>
				</tbody>
				</table>
	  </td>			
	</tr>
			
	</c:forEach>
   
   </tbody>
	</table>
   
</body>
</html>
